import Vue from 'vue'
import VueRouter from 'vue-router'
// import login from '../components/Login.vue'
const login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')

// import home from '../components/Home.vue'
const home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')

// import welcome from '../components/Welcome.vue'
const welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

// import users from '../components/user/Users.vue'
const users = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/Users.vue')

// import rights from '../components/power/rights.vue'
const rights = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/rights.vue')

// import roles from '../components/power/Roles.vue'
const roles = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/Roles.vue')

// import cate from '../components/goods/Cate.vue'
const cate = () => import(/* webpackChunkName: "cate_Params" */ '../components/goods/Cate.vue')

// import params from '../components/goods/Params.vue'
const params = () => import(/* webpackChunkName: "cate_Params" */ '../components/goods/Params.vue')


// import GoodsList from '../components/goods/List.vue'
const GoodsList = () => import(/* webpackChunkName: "GoodList_add" */ '../components/goods/List.vue')

// import Add from '../components/goods/Add.vue'
const Add = () => import(/* webpackChunkName: "GoodList_add" */ '../components/goods/Add.vue')

// import Order from '../components/order/Order.vue'
const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')

// import Report from '../components/report/Report.vue'
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles },
      { path: '/categories', component: cate },
      { path: '/params', component: params }, 
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
// 前置守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路劲跳转过来
  // next 是一个函数,标识放行
  // 访问路径是login 直接放行
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  // 没有token 跳转到登录页面
  if (!token) return next('/login')
  // 有token放行
  next()
})
export default router
